package com.netease.nim.demo.session.audio;

import android.text.TextUtils;

import com.netease.nim.demo.common.media.audioplayer.Playable;
import com.netease.nimlib.sdk.msg.model.IMMessage;

public class PlayableFile implements Playable {

	private String path;

	private long duration;

	private IMMessage attachMessage;

	public void setAttachMessage(IMMessage attachMessage) {
		this.attachMessage = attachMessage;
	}

	public IMMessage getAttachMessage() {
		return attachMessage;
	}

	public PlayableFile(String path, long duration) {
		this.path = path;
		this.duration = duration;
		this.attachMessage = null;
	}

	public PlayableFile(String path, long duration, IMMessage attachMessage) {
		this.path = path;
		this.duration = duration;
		this.attachMessage = attachMessage;
	}

	@Override
	public long getDuration() {
		return duration;
	}

	@Override
	public String getPath() {
		return path;
	}

	@Override
	public boolean isAudioEqual(Playable audio) {

		if (PlayableFile.class.isInstance(audio)) {
			PlayableFile playableFile = (PlayableFile) audio;

			if ((this.getAttachMessage() == null && playableFile.getAttachMessage() != null)
					|| (this.getAttachMessage() != null && playableFile.getAttachMessage() == null)) {
				return false;
			} else if (this.getAttachMessage() == null && playableFile.getAttachMessage() == null) {
				if (TextUtils.isEmpty(audio.getPath()) || TextUtils.isEmpty(getPath())) {
					return false;
				} else {
					return audio.getPath().equals(getPath());
				}
			} else {
				if (TextUtils.isEmpty(audio.getPath()) || TextUtils.isEmpty(getPath())) {
					return false;
				} else {
					return audio.getPath().equals(getPath())
							&& (this.getAttachMessage().isTheSame(playableFile.getAttachMessage()));
				}
			}
		} else if (MsgHistoryPlayableWrapper.class.isInstance(audio)) {
			IMMessage attachMessage = getAttachMessage();
			if (attachMessage != null) {
				return attachMessage.isTheSame(((MsgHistoryPlayableWrapper) audio)
                        .getPlayableMessageHistory());
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

}
